
<template>
  <div class="execution">
    <basic-container>
      <avue-crud
        :permission="permissionList"
        v-model="form"
        v-bind="bindVal"
        v-on="onEvent"
        :page.sync="page"
        :before-open="beforeOpen"
      >
        <template slot="menuLeft">
          <el-button type="primary" size="small" @click="delBatchs"
            v-permission="permissionList.delBatchBtn"
            >删除</el-button
          >
        </template>
      </avue-crud>
    </basic-container>
  </div>
</template>

<script>
import {
  fetchList,
  getObj,
  addObj,
  putObj,
  delObj,
  delBatch
} from "@/api/subwaystm/swstotherfare";
import { tableOption } from "@/const/crud/subwaystm/swstotherfare";
import { mapGetters } from "vuex";
import baseCrud from "@/mixins/base-crud";
import { dateFormat } from "@/util/date";

export default {
  name: "swstotherfare",
  mixins: [baseCrud("subwaystm/swstotherfare")],
  data() {
    return {
      form: {},
    };
  },
  watch: {
    // 'form.entryName':function(){
    //     this.form.entryCode=this.form.entryName;
    // }
  },

  computed: {
    ...mapGetters(["permissions"]),
    permissionList() {
      return {
        addBtn: this.vaildData(this.permissions.stm_swstotherfare_add, false),
        delBtn: this.vaildData(this.permissions.stm_swstotherfare_del, false),
        editBtn: this.vaildData(this.permissions.stm_swstotherfare_edit, false),
        delBatchBtn: this.vaildData(this.permissions.stm_swstotherfaret_delBatch,false)
      };
    },
  },
  methods: {
    beforeOpen(done, type) {
      if (type == "add") {
        setTimeout(() => {
          let now = new Date();
          if (now.getHours() < 6) {
            now.setDate(now.getDate() - 1);
          }
          this.form.entryDate = dateFormat(now).substring(0, 10);
        }, 0);
        done();
      } else {
        getObj(this.form.id).then((response) => {
          this.form = response.data.data;
          if (type != "view") {
            this.form.$cellEdit = true;
          }
          done();
        });
      }
    },
    rowSave: function (row, done, loading) {
      if (
        this.validatenull(this.form.otherMoney) &&
        this.validatenull(this.form.customerHandoverLong)
      ) {
        this.$message.info("其他票款金额和交接长款必须填写一个！");
        loading();
        return false;
      }
      const callback = () => {
        addObj(row)
          .then((data) => {
            this.$message.success("添加成功");
            done();
            this.getList(this.page);
          })
          .catch(() => {
            loading();
          });
      };
      if (typeof this.beforeRowSave == "function") {
        this.beforeRowSave(row, callback, loading);
      } else {
        callback();
      }
    },
    rowUpdate: function (row, index, done, loading) {
      if (
        this.validatenull(this.form.otherMoney) &&
        this.validatenull(this.form.customerHandoverLong)
      ) {
        this.$message.info("其他票款金额和交接长款必须填写一个！");
        loading();
        return false;
      }
      const callback = () => {
        putObj(row)
          .then((data) => {
            this.$message.success("修改成功");
            done();
            this.getList(this.page);
          })
          .catch(() => {
            loading();
          });
      };
      if (typeof this.beforeRowUpdate == "function") {
        this.beforeRowUpdate(row, index, callback, loading);
      } else {
        callback();
      }
    },
    delBatchs: function () {
      if (this.$refs.crud.tableSelect.length < 1) {
        this.$message.error("请选择数据！");
        return false;
      }
      let ids = [];
      for (const table of this.$refs.crud.tableSelect) {
        ids.push(table.id);
      }
      this.$confirm("确认删除?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        delBatch(ids)
          .then((data) => {
            this.$message.success("删除成功");
            this.getList(this.page);
            done();
          })
          .catch(() => {
            loading();
          });
      });
    },
  }
}
</script>
